﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using EntityObject;
using SharedLibraries;
using SharedLibraries.Constants;
using ThangNM_LIB.DB;
using ThangNM_LIB.FUNC;

namespace ThangNM.UserControl.Admin
{
    public partial class NewsLetterUC : System.Web.UI.UserControl
    {
        public List<NewLetterEO> lstNewLetterEO
        {
            get { return (List<NewLetterEO>)ViewState["lstNewLetterEO"]; }
            set { ViewState["lstNewLetterEO"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Title_FormUC1.setTitle(ConstEx.Table_Name_C.NewsLetter, ConstEx.funcButtonToolBar.LIST);
            MsgUC1.setClearMsg();
            if (!IsPostBack)
            {
                //if ((new ShareDAO.UserDAO()).SelectBy_sPermission(Session["secret"], Convert.ToString(Request.Url.AbsolutePath), ConstEx.funcButtonToolBar.MAIN) == false)
                //{
                //    grvError.Visible = false;
                //    Commons.setAccessDenied(Page, MsgUC1);
                //    return;
                //}
                getData();
            }
        }

        protected void getData()
        {
            try
            {
                grvError.Visible = false;
                String StrPath = System.Web.HttpContext.Current.Server.MapPath("~/GlobalResources/Email.json");
                if (!String.IsNullOrEmpty(StrPath))
                {
                    StrPath = System.IO.File.ReadAllText(StrPath);
                    lstNewLetterEO = new List<NewLetterEO>();
                    object temp = JsonEx.Json2ListObject(StrPath, lstNewLetterEO.GetType());
                    if (temp != null)
                        lstNewLetterEO = (List<NewLetterEO>)temp;
                    var reducedList = lstNewLetterEO.Select(item => new { item.sEmail, item.tRegisterDate }).ToList();
                    grvError.Visible = true;
                    grvError.DataSource = reducedList;
                    grvError.DataBind();
                }
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void grvError_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            var reducedList = lstNewLetterEO.Select(item => new { item.sEmail, item.tRegisterDate }).ToList();
            grvError.PageIndex = e.NewPageIndex;
            grvError.DataSource = reducedList;
            grvError.DataBind();
        }
    }
}